import socket
from threading import *

class Receiver(Thread):
	def __init__(self, socket):
		Thread.__init__(self)
		self.socket = socket
		self.listeners = []
		self.lck = Lock()

	def addListener(self, l):
		self.lck.acquire()
		self.listeners.append(l)
		self.lck.release()

	def removeListener(self, l):
		self.lck.acquire()
		self.listeners.remove(l)
		self.lck.release()

	def notifyListeners(self, str):
		self.lck.acquire()
		for i in self.listeners:
			i.notify(str)
		self.lck.release()

	def run(self):
		while True:
			str = self.socket.recv(1024)
			self.notifyListeners(str)
